﻿@inherits OwningComponentBase<ICovenantService>

@using Covenant.Core
@using Covenant.Models.Launchers
@using Covenant.Components.Shared

<CovenantTable TItem="Launcher" Items="Launchers.ToList()" SearchRow="IsRowDisplayed"
               IsSearchable="IsSearchable" IsSortable="IsSortable" IsPaginated="IsPaginated" PageLength="PageLength" class="table table-hover">
    <TableHeader>
        <SortableTableHeader TItem="Launcher" TItem2="string" GetSortableProperty="(launcher) => launcher.Name">
            <HeaderContent>Name</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="Launcher" TItem2="string" GetSortableProperty="(launcher) => launcher.Description">
            <HeaderContent>Description</HeaderContent>
        </SortableTableHeader>
    </TableHeader>
    <TableRow Context="launcher">
        <tr>
            <td>
                <a href="/launcher/create/@launcher.Id">
                    @launcher.Name
                </a>
            </td>
            <td>@launcher.Description</td>
        </tr>
    </TableRow>
</CovenantTable>

@code {
    [Parameter]
    public IEnumerable<Launcher> Launchers { get; set; }

    [Parameter]
    public bool IsSearchable { get; set; } = false;

    [Parameter]
    public bool IsSortable { get; set; } = true;

    [Parameter]
    public bool IsPaginated { get; set; } = false;

    [Parameter]
    public int PageLength { get; set; } = 25;

    protected override async Task OnInitializedAsync()
    {
        this.Launchers = this.Launchers ?? await Service.GetLaunchers();
    }

    private bool IsRowDisplayed(Launcher launcher, string SearchTerm)
    {
        return SearchTerm == string.Empty ||
            launcher.Name.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            launcher.Description.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase);
    }
}
